#ifndef LOG_LOGGER_H
#define LOG_LOGGER_H

#include "../../base/TimeUnit.hpp"
#include "Buffer.hpp"
#include "CountDownLatch.hpp"

#include <vector>
#include <string>
#include <thread>
#include <mutex>
#include <atomic>
#include <condition_variable>

namespace DBServer
{
namespace Log
{

class Logger {
public:

    Logger(int _roll_size, int _flush_inteval = 3);
    ~Logger();

    void debug(std::string&&);
    void info(std::string&&);
    void warn(std::string&&);
    void error(std::string&&);

    void start(int log_buffer_size);
    void stop();

private:
    // logger deamon thread function
    void thread_func(int log_buffer_size);
    void append(std::string&&, int);
    auto log_factory(std::string&, int) -> void;

private:
    using BufferVector = std::vector<std::unique_ptr<Buffer>>;
    using BufferPtr = std::unique_ptr<Buffer>;

    std::thread t;
    std::mutex mtx;
    std::atomic<bool> running;
    std::condition_variable cond;
    CountDownLatch latch;

    BufferPtr cur_buf, next_buf;
    BufferVector buffers;

    int flush_interval;
    int roll_size;

    // clock
    DBServer::Base::Timer::Time clock;
};

} // namespace Log
} // namespace DBServer



#endif